<%@page import="lab.bmp.studio.Studio"%>
<%@page import="lab.bmp.genre.GenrePK"%>
<%@page import="lab.bmp.genre.GenrePK"%>
<%@page import="lab.bmp.artist.ArtistPK"%>
<%@page import="java.util.*"%>
<%@page import="lab.bmp.track.Track"%>
<%@page import="lab.bmp.genre.Genre"%>
<%@page import="lab.bmp.artist.Artist"%>
<%@page extends="lab.jsp.EditTrack" errorPage="error_page.jsp"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%

    Track track = getData();

    String artists_select = "";
    Iterator<Artist> iter = getArtists().iterator();
    LinkedList<Integer> artists = new LinkedList<Integer>();
    while (iter.hasNext()) {
        Artist artist = iter.next();
        artists.add(artist.getId());
        artists_select += "<option value=" + artist.getId() + ">" + artist.getName() + "</option>";
    }
    artists_select += "</select>";

    String genres_select = "";
    Iterator<Genre> iter2 = getGenres().iterator();
    LinkedList<Integer> genres = new LinkedList<Integer>();
    while (iter2.hasNext()) {
        Genre genre = iter2.next();
        genres.add(genre.getId());
        genres_select += "<option value=" + genre.getId() + ">" + genre.getName() + "</option>";
    }
    genres_select += "</select>";

    String studios_select = "<select name=\"studio\" id=\"studio\">";
    int index = 0;
    int studioPosition = 0;
    Iterator<Studio> iter3 = getStudios().iterator();
    while (iter3.hasNext()) {
        Studio studio = iter3.next();
        if (track.getStudio().getId().intValue() == studio.getId()) {
            studioPosition = index;
        }
        studios_select += "<option value=" + studio.getId() + ">" + studio.getName() + "</option>";
        index++;
    }
    studios_select += "</select>";

%>
<style>
    p { margin: 0px; }
</style>
<script>
    
    var artists = "<%=artists_select%>";
    var genres = "<%=genres_select%>";
 
    var artist_count = -1;
    var genre_count = -1;

    function delElem(elem) {
        elem.parentNode.removeChild(elem);
    }

    function add_artist() {
        artist_count++;
        var artists_elem = document.getElementById("artists_block");
        var postfix = "["+artist_count+"]";
        var new_p =  document.createElement("p");
        new_p.id = "partists"+postfix;
        new_p.innerHTML += "<select name='artists"+postfix+"' id='artists"+postfix+"'>" + artists;
        new_p.innerHTML += "&nbsp;<a href=\"#\" onClick='del_artist("+artist_count+")' name='a_artists"+postfix+"'>Delete</a>"
        artists_elem.appendChild(new_p);
        document.getElementById("artists_count").value = artist_count + 1;
    }

    function add_genre() {
        genre_count++;
        var genres_elem = document.getElementById("genres_block");
        var postfix = "["+genre_count+"]";
        var new_p =  document.createElement("p");
        new_p.id = "pgenres"+postfix;
        new_p.innerHTML += "<select name='genres"+postfix+"' id='genres"+postfix+"'>" + genres;
        new_p.innerHTML += "&nbsp;<a href=\"#\" onClick='del_genre("+genre_count+")' name='a_genres"+postfix+"'>Delete</a>"
        genres_elem.appendChild(new_p);
        document.getElementById("genres_count").value = genre_count + 1;
    }

    function del_artist(i) {

        if (i < artist_count) {
            for (j = i; j < artist_count; j++) {
                var current_input = document.getElementById("artists[" + j + "]");
                var next_input = document.getElementById("artists[" + (j+1) + "]");
                current_input.selectedIndex = next_input.selectedIndex;
                current_input.value = next_input.value;
            }
        }
	
        var postfix = "["+artist_count+"]";
        delElem(document.getElementById("partists" + postfix));
	
        artist_count--;
            
        document.getElementById("artists_count").value = artist_count;
            
    }
        
    function del_genre(i) {

        if (i < genre_count) {
            for (j = i; j < genre_count; j++) {
                var current_input = document.getElementById("genres[" + j + "]");
                var next_input = document.getElementById("genres[" + (j+1) + "]");
                current_input.selectedIndex = next_input.selectedIndex;
                current_input.value = next_input.value;
            }
        }
	
        var postfix = "["+genre_count+"]";
        delElem(document.getElementById("pgenres" + postfix));
	
        genre_count--;
        document.getElementById("genres_count").value = genre_count + 1;
    }
    
</script>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <a href="index.jsp">to main</a><br>
        <h2>Track</h2>
        <form name="form1" method="post" action="edit_track.jsp">
            <table>
                <tr><td>Caption:</td><td><input type="text" name="name" value="<%=track.getName()%>"/></td></tr>
                <tr><td>Year:</td><td><input type="text" name="year" value="<%=track.getYear()%>"/></td></tr>
                <tr><td>Length:</td><td><input type="text" name="length" value="<%=track.getLength()%>"/></td></tr>
                <tr><td>Studio:</td><td><%=studios_select%></td></tr>
            </table>
            <input name="id" type="hidden" value="<%=track.getId()%>"/>
            <input name="artists" id="artists_count" type="hidden" value="<%=track.getArtists().size()%>"/>
            <input name="genres" id="genres_count" type="hidden" value="<%=track.getGenres().size()%>"/>
            <br><b>Artists:</b>
            <p id="artists_block"></p>
            <a href="#" onClick="add_artist()">Add artist</a>
            <br><b>Genres:</b>
            <p id="genres_block"></p>
            <a href="#" onClick="add_genre()">Add genre</a>
            <br/><br/>
            <input type="submit" name="save" value="Save"/>
            <% if (track.getId() >= 0) {%>
            <input type="submit" name="delete" value="Delete"/>
            <% }%>
        </form>
    </body>
    <script>
        document.getElementById("studio").selectedIndex = <%=studioPosition%>;
        <%
            int artist_index = -1;
            for (ArtistPK a : track.getArtists()) {
                Artist artist = dao.findArtist(a);
                artist_index++;
        %>
            add_artist();
            document.getElementById("artists[<%=artist_index%>]").selectedIndex = <%=artists.indexOf(a.getId())%>;
        <%
            }
            int genres_index = -1;
            for (GenrePK g : track.getGenres()) {
                Genre genre = dao.findGenre(g);
                genres_index++;
        %>
            add_genre();
            document.getElementById("genres[<%=genres_index%>]").selectedIndex = <%=genres.indexOf(g.getId())%>;
        <%
            }
        %>
    </script>
</html>
